package com.zkyc.framework.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkyc.framework.common.constant.Constant;
import com.zkyc.framework.common.dto.PageDto;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.mapper.*;
import com.zkyc.framework.common.pojo.*;
import com.zkyc.framework.common.utils.DecryptDataUtil;
import com.zkyc.framework.common.utils.Md5Util;
import com.zkyc.framework.common.utils.WebsiteUtil;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.vo.role.RoleVo;
import com.zkyc.framework.common.vo.user.*;
import com.zkyc.framework.common.vo.website.WebVo;
import com.zkyc.framework.common.vo.website.WebsiteVo;
import com.zkyc.framework.user.Util.TokenUserUtil;
import com.zkyc.framework.user.config.JedisPoolFactory;
import com.zkyc.framework.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;


/**
 * (User)表服务实现类
 *
 * @since 2022-05-17 15:44:59
 */
@Slf4j
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRefRoleMapper userRefRoleMapper;

    @Value("${secretKey}")
    private String secretKey;

    @Autowired
    private WebsiteMapper websiteMapper;

    @Autowired
    private RoleServiceImpl roleService;

    @Autowired
    private RightMapper rightMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleRefMenuMapper roleRefMenuMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private CenterMapper centerMapper;


    //根据用户id查询用户
    @Override
    public User queryById(String id) {
        return this.userMapper.selectById(id);
    }

    //用户登录
    @Override
    public ResultVO login(UserVo userVo, HttpSession session) {

        //1.参数校验
        if (userVo == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (userVo.getUuid() == null && userVo.getVerification() == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        String codeKey = "verifyCode_" + userVo.getUuid();
        String code = JedisPoolFactory.select(codeKey);
        if (StringUtils.isEmpty(code)) {
            return ResultVO.fail("验证码已过期!");
        }
        if (!code.equals(userVo.getVerification())) {
            JedisPoolFactory.delete(codeKey);
            return ResultVO.fail("验证码错误!");
        }
        String password = DecryptDataUtil.getDecryptData(secretKey, userVo.getPassword());
        //2.查询数据库中的用户信息,判断用户是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", userVo.getUsername());
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            JedisPoolFactory.delete(codeKey);
            return ResultVO.fail(ResultCode.PASSWORD_ERROR);
        }
        //3.判断用户是否被禁用
        if (user.getStatus() == 0) {
            JedisPoolFactory.delete(codeKey);
            return ResultVO.fail(503, "该用户已被禁用!");
        }
        //4.如果用户没有角色,则用户不能登录(排除超级管理员)
        if (!Constant.SUPERADMINUSERID.equals(user.getId())) {
            List<UserRefRole> userRefRoleList = userRefRoleMapper.selectList(new QueryWrapper<UserRefRole>().eq("user_id", user.getId()));
            if (CollectionUtils.isEmpty(userRefRoleList)) {
                JedisPoolFactory.delete(codeKey);
                //判断密码是否正确,正确把登录的用户信息放到redis
                String md5Password = DigestUtils.md5Hex(password);
                if (user.getPassword().equals(md5Password)) {
                    //5.通过BCrypt生成token,并将用户信息存入redis中
                    String token = BCrypt.hashpw("", BCrypt.gensalt());
                    String redisKey = Constant.REDIS_PREFIX + "TOKEN_" + token;
                    System.out.println(token);
                    JedisPoolFactory.setex(redisKey, 7200, JSONObject.toJSONString(user));
                    Map<String, Object> map = new HashMap<>();
                    user.setPassword("");
                    map.put("user", user);
                    map.put("token", token);
                    map.put("WebsiteList", new ArrayList<>());
                    JedisPoolFactory.delete(codeKey);
                    return ResultVO.data(map);
                }
            }
            //判断角色是否被禁用
            String status = "0";
            for (UserRefRole userRefRole : userRefRoleList) {
                Role role = roleMapper.selectById(userRefRole.getRoleId());
                if (role.getStatus() == 1) {
                    status = "1";
                    break;
                }
            }
            if (status.equals("0")) {
                JedisPoolFactory.delete(codeKey);
                return ResultVO.fail(503, "该用户角色已被禁用!");
            }

        }
        String lockKey = Constant.REDIS_PREFIX + "LOCK_" + user.getId();
        String lockCount = JedisPoolFactory.select(lockKey);
        //判断该账号是否被锁定
        if (lockCount == null) {
            lockCount = "1";
        }
        if (Integer.parseInt(lockCount) >= 5) {
            JedisPoolFactory.delete(codeKey);
            return ResultVO.fail(503, "该账号已经被锁定,请稍后重试!");
        }

        //5.判断密码是否正确
        String md5Password = DigestUtils.md5Hex(password);
        if (user.getPassword().equals(md5Password)) {
            //5.通过BCrypt生成token,并将用户信息存入redis中
            String token = BCrypt.hashpw("", BCrypt.gensalt());
            String redisKey = Constant.REDIS_PREFIX + "TOKEN_" + token;
            System.out.println(token);
            JedisPoolFactory.setex(redisKey, 3600, JSONObject.toJSONString(user));
            //6.通过用户id查询网站id
            List<WebsiteVo> websiteList = new ArrayList<>();
            if (Constant.SUPERADMINUSERID.equals(user.getId())) {
                List<Website> websites = websiteMapper.selectList(new QueryWrapper<Website>().eq("status", 1));
                for (Website website : websites) {
                    WebsiteVo websiteVo = new WebsiteVo();
                    websiteVo.setWebsiteId(website.getId());
                    websiteVo.setWebsiteName(website.getWebsiteName());
                    websiteList.add(websiteVo);
                }
            } else {
                websiteList = userMapper.getWebsiteListByUserId(user.getId());
            }
            if (CollectionUtils.isEmpty(websiteList)) {
                JedisPoolFactory.delete(codeKey);
                return ResultVO.fail(500, "网站被禁用无法登陆");
            }
            //7.封装结果
            Map<String, Object> map = new HashMap<>();
            user.setPassword("");
            map.put("user", user);
            map.put("token", token);
            map.put("WebsiteList", websiteList);
            JedisPoolFactory.delete(codeKey);
            return ResultVO.data(map);
        } else {
            String count = JedisPoolFactory.select(lockKey);
            if (count == null) {
                JedisPoolFactory.setex(lockKey, 3600, "0");
            } else {
                JedisPoolFactory.setex(lockKey, 3600, Integer.parseInt(count) + 1 + "");
            }
            JedisPoolFactory.delete(codeKey);
            return ResultVO.fail(503, "用户名或密码错误!");
        }
    }

    //修改密码
    @Override
    public ResultVO changePassword(ChangePasswordVo changePasswordVo) {
        if (changePasswordVo.getUsername() == null &&
                changePasswordVo.getPassword() == null &&
                changePasswordVo.getNewPassword() == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        String password = DecryptDataUtil.getDecryptData(secretKey, changePasswordVo.getPassword());
        String newPassword = DecryptDataUtil.getDecryptData(secretKey, changePasswordVo.getNewPassword());
        if (password.equals(newPassword)) {
            return ResultVO.fail(503, "新旧密码不能一致");
        }
        String md5Password = Md5Util.getMd5(password);
        String md5NewPassword = Md5Util.getMd5(newPassword);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", changePasswordVo.getUsername());
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        if (!user.getPassword().equals(md5Password)) {
            return ResultVO.fail(503, "密码错误");
        }
        user.setPassword(md5NewPassword);
        userMapper.update(user, wrapper);


        return ResultVO.success(ResultCode.SUCCESS);
    }

    //登出
    @Override
    public ResultVO logout(ServletRequest servletRequest) {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //得到header中的信息
        String token = request.getHeader("accessToken");
        String redisKey = Constant.REDIS_PREFIX + "TOKEN_" + token;
        JedisPoolFactory.delete(redisKey);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    //获取用户列表
    @Override
    public ResultVO getUserList(UserPageVo userPageVo) {
        if (userPageVo == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        //分页参数校验
        PageDto pageDto = new PageDto();
        BeanUtils.copyProperties(userPageVo, pageDto);
        pageDto.checkParam();
        Page page = new Page(pageDto.getPage(), pageDto.getSize());
        Page<User> userList;
        if (Constant.SUPERADMINUSERID.equals(userPageVo.getUserId())) {
            userList = userMapper.selectPage(page, new QueryWrapper<User>().orderByDesc("gmt_updated"));
        } else {
            userList = userMapper.getUserListByWebsiteId(userPageVo.getUserId(), page);
        }
        return ResultVO.data(userList);
    }

    //判断用户是否是管理员用户
    @Override
    public ResultVO judgeAdminUser(UserIdVo userIdVo) {
        if (userIdVo == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        List<String> userIdList = userIdVo.getUserIdList();
        List<String> userIds = new ArrayList<>();
        for (String userId : userIdList) {
            userIds.add("'" + userId + "'");
        }
        String s = String.join(",", userIds);
        List<JudgeUserVo> judgeUserVos = userMapper.judgeAdmin(s, userIdVo.getWebsiteId());
        Map<String, Integer> judgeAdminMap = new HashMap<>();
        for (JudgeUserVo judgeUserVo : judgeUserVos) {
            if (judgeAdminMap.get(judgeUserVo.getUserId()) == null) {
                judgeAdminMap.put(judgeUserVo.getUserId(), judgeUserVo.getIsAdmin());
            } else {
                judgeAdminMap.put(judgeUserVo.getUserId(), 0);
            }
        }
        return ResultVO.data(judgeAdminMap);
    }

    //查看按钮展示用户的权限信息
    @Override
    public ResultVO getRoleListByUserId(String userId) {
        if (userId == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        String roleNames;
        if (Constant.SUPERADMINUSERID.equals(userId)) {
            roleNames = roleMapper.selectList(null).stream().map(Role::getRoleName).collect(Collectors.joining(","));
        } else {
            List<UserSelfRoleListVo> userSelfRoleList = userMapper.getUserSelfRoleList(userId);
            List<String> roleList = userSelfRoleList.stream().map(UserSelfRoleListVo::getRoleName).collect(Collectors.toList());
            roleNames = String.join(",", roleList);
        }
        User user = queryById(userId);
        if (user == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("username", user.getUsername());
        resultMap.put("remark", user.getRemark());
        resultMap.put("roleNames", roleNames);
        return ResultVO.data(resultMap);
    }

    //新增用户
    @Override
    public ResultVO insert(UserRoleVo userRoleVo) {
        if (userRoleVo == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        //如果用户名已存在,返回错误信息
        User user1 = userMapper.selectOne(new QueryWrapper<User>().eq("username", userRoleVo.getUsername()));
        if (user1 != null) {
            return ResultVO.fail("该账号已存在");
        }
        //新增用户
        User user = new User();
        //添加中心
        if (userRoleVo.getCenterId() != null) {
            Center center = centerMapper.selectById(userRoleVo.getCenterId());
            user.setCenterId(userRoleVo.getCenterId());
            user.setCenterName(center.getCenterName());
        }
        //添加研究院
        if (userRoleVo.getOrganizationId() != null) {
            Organization organization = organizationMapper.selectById(userRoleVo.getOrganizationId());
            user.setOrganizationId(userRoleVo.getOrganizationId());
            user.setOrganizationName(organization.getOrganizationName());
        }
        user.setId(UUID.randomUUID().toString());
        user.setUsername(userRoleVo.getUsername());
        user.setPassword(Md5Util.getMd5("123456"));
        String currentUserId = TokenUserUtil.getCurrentUserId();
        user.setCreatorName(userMapper.selectById(currentUserId).getUsername());
        user.setGmtCreated(new Date());
        user.setGmtUpdated(new Date());
        user.setRemark(userRoleVo.getRemark());
        user.setStatus(1);
        user.setIsDeleted(0);
        userMapper.insert(user);
        userRoleVo.getRoleList().forEach(s -> {
                    //插入用户角色中间表
                    UserRefRole userRefRole = new UserRefRole();
                    userRefRole.setUserId(user.getId());
                    userRefRole.setRoleId(s);
                    userRefRole.setGmtCreated(new Date());
                    userRefRoleMapper.insert(userRefRole);
                }
        );
        return ResultVO.success("保存成功,已启用");
    }

    //编辑用户
    @Override
    public ResultVO update(UserRoleVo userRoleVo) {
        if (userRoleVo == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        //删除旧绑定关系
        HashMap map = new HashMap();
        map.put("user_id", userRoleVo.getId());
        userRefRoleMapper.deleteByMap(map);
        //新增绑定关系
        userRoleVo.getRoleList().forEach(s -> {
                    //插入用户角色中间表
                    UserRefRole userRefRole = new UserRefRole();
                    userRefRole.setUserId(userRoleVo.getId());
                    userRefRole.setRoleId(s);
                    userRefRole.setGmtCreated(new Date());
                    userRefRoleMapper.insert(userRefRole);
                }
        );
        User user = userMapper.selectById(userRoleVo.getId());
        //添加中心
        if (userRoleVo.getCenterId() != null) {
            Center center = centerMapper.selectById(userRoleVo.getCenterId());
            user.setCenterId(userRoleVo.getCenterId());
            user.setCenterName(center.getCenterName());
        } else {
            user.setCenterId(null);
            user.setCenterName(null);
        }
        //添加研究院
        if (userRoleVo.getOrganizationId() != null) {
            Organization organization = organizationMapper.selectById(userRoleVo.getOrganizationId());
            user.setOrganizationId(userRoleVo.getOrganizationId());
            user.setOrganizationName(organization.getOrganizationName());
        } else {
            user.setOrganizationId(null);
            user.setOrganizationName(null);
        }

        user.setRemark(userRoleVo.getRemark());
        user.setGmtUpdated(new Date());
        userMapper.updateById(user);
        return ResultVO.success(ResultCode.SUCCESS);
    }


    @Override
    public ResultVO deleteById(String id) {
        if (id == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(id) == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        userMapper.deleteById(id);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    @Override
    public ResultVO resetPassword(String userId) {
        if (userId == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        User user = queryById(userId);
        if (user == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        user.setPassword(Md5Util.getMd5("123456"));
        userMapper.updateById(user);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    @Override
    public ResultVO getRightByWebsite(Integer websiteId) {
        if (websiteId == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        List<Integer> roleIdList = roleMapper.getRoleIdListByWebsiteId(websiteId.longValue());
        List<RightVo> authorization = roleService.getAuthorization(roleIdList);

        return ResultVO.data(authorization);
    }


    @Override
    public ResultVO getRoleByUserId(String userId) {
        if (userId == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        List<WebsiteVo> websiteVos = new ArrayList<>();
        List<WebsiteVo> websiteList = userMapper.getWebsiteListByUserId(userId);
        websiteList.forEach(s -> {
            WebsiteVo websiteVo = new WebsiteVo();
            websiteVo.setWebsiteId(s.getWebsiteId());
            List<RoleVo> roleList = roleMapper.getRoleList(s.getWebsiteId().longValue());
            websiteVo.setWebsiteName(s.getWebsiteName());
            websiteVo.setRoleList(roleList);
            websiteVos.add(websiteVo);
        });
        return ResultVO.data(websiteVos);
    }

    @Override
    public ResultVO queryRightByUserId(String userId) {
        if (userId == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        User user = queryById(userId);
        if (user == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        List<UserSelfRoleListVo> userSelfRoleList = userMapper.getUserSelfRoleList(userId);
        Set<WebsiteVo> websiteVos = new HashSet<>();
        Set<WebVo> webVos = new HashSet<>();
        for (UserSelfRoleListVo userSelfRoleListVo : userSelfRoleList) {
            WebVo webVo = new WebVo();
            BeanUtils.copyProperties(userSelfRoleListVo, webVo);
            webVos.add(webVo);
        }
        for (WebVo webVo : webVos) {
            WebsiteVo websiteVo = new WebsiteVo();
            List<RoleVo> roleList = new ArrayList<>();
            userSelfRoleList.forEach(s -> {
                        if (s.getWebsiteId().equals(webVo.getWebsiteId())) {
                            RoleVo roleVo = new RoleVo();
                            roleVo.setRoleId(s.getRoleId());
                            roleVo.setRoleName(s.getRoleName());
                            roleList.add(roleVo);
                        }
                    }
            );
            websiteVo.setWebsiteId(webVo.getWebsiteId());
            websiteVo.setWebsiteName(webVo.getWebsiteName());
            websiteVo.setRoleList(roleList);
            websiteVos.add(websiteVo);
        }
        return ResultVO.data(websiteVos);
    }

    @Override
    public ResultVO forbidden(String userId, Integer status) {
        if (userId == null || status == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        User user = queryById(userId);
        user.setStatus(status);
        userMapper.update(user, new QueryWrapper<User>().eq("id", userId));
        return ResultVO.success(ResultCode.SUCCESS);
    }

    @Override
    public ResultVO getUserRightByWebsite(Integer websiteId) {
        String currentUserId = TokenUserUtil.getCurrentUserId();
        List<Right> userWebRight;
        List<Right> userWebRightTree;
        int isAdmin = 0;
        if (Constant.SUPERADMINUSERID.equals(currentUserId)) {
            isAdmin = 2;
            userWebRight = rightMapper.selectList(null);
            //去除superadmin中一些不要的路由
            Iterator<Right> iterator = userWebRight.iterator();
            while (iterator.hasNext()) {
                Right next = iterator.next();
                String name = next.getName();
                //只有之江实验室官网需要稿件管理,其他的都去除
                if (websiteId != 1 && name.equals("稿件管理")) {
                    iterator.remove();
                }
                //只有博士后官网需要招聘宣讲
                if (websiteId != 2 && name.equals("招聘宣讲")) {
                    iterator.remove();
                }
                //只有工会之家需要协会管理
                if (websiteId != 4 && name.equals("协会管理")) {
                    iterator.remove();
                }
                //工会之家不要组织管理
                if (websiteId == 4 && name.equals("组织管理")) {
                    iterator.remove();
                }
            }
            userWebRightTree = roleService.getRightTree(userWebRight);
        } else {
            List<Integer> rightIds = userRefRoleMapper.getUserWebRight(currentUserId, websiteId).stream().map(Right::getId).collect(Collectors.toList());
            String rightListString = rightIds.stream().map(Objects::toString).collect(Collectors.joining(","));
            if (StringUtils.isNotEmpty(rightListString)) {
                List<Right> rightAndParent = userRefRoleMapper.getRightAndParent(rightListString);
                //只有之江实验室官网需要稿件管理,其他的都去除
                Iterator<Right> iterator = rightAndParent.iterator();
                while (iterator.hasNext()) {
                    Right next = iterator.next();
                    String name = next.getName();
                    if (websiteId != 1 && name.equals("稿件管理")) {
                        iterator.remove();
                    }
                    if (websiteId == 4 && name.equals("组织管理")) {
                        iterator.remove();
                    }
                }
                userWebRightTree = roleService.getRightTree(rightAndParent);
            } else {
                userWebRightTree = new ArrayList<>();
            }
        }
        List<Role> roles = roleMapper.queryRoleByUserIdWebsiteId(currentUserId, websiteId);
        for (int i = 0; i < roles.size(); i++) {
            if (roles.get(i).getRoleType() == 0) {
                isAdmin = 1;
                break;
            }
        }
        HashMap<Object, Object> result = new HashMap<>();
        result.put("list", userWebRightTree);
        result.put("isAdmin", isAdmin);
        return ResultVO.data(result);
    }

    @Override
    public ResultVO judgeRightExist(Integer websiteId, String userId) {
        Map<String, Boolean> judgeMap = new HashMap<>();
        judgeMap.put("content", false);
        judgeMap.put("column", false);
        judgeMap.put("media", false);
//        if (Constant.SUPERADMINUSERID.equals(userId)) {
//            judgeMap.forEach((k, v) -> {
//                judgeMap.put(k, true);
//            });
//            return ResultVO.data(judgeMap);
//        }
        List<Right> userRightList;
        if (Constant.SUPERADMINUSERID.equals(userId)) {
            userRightList = rightMapper.selectList(null);

        } else {
            userRightList = userRefRoleMapper.getUserWebRight(userId, websiteId);
        }
        Iterator<Right> iterator = userRightList.iterator();
        while (iterator.hasNext()) {
            Right next = iterator.next();
            String name = next.getName();
            //只有之江实验室官网需要稿件管理,其他的都去除
            if (websiteId != 1 && name.equals("稿件管理")) {
                iterator.remove();
            }
            //只有博士后官网需要招聘宣讲
            if (websiteId != 2 && name.equals("招聘宣讲")) {
                iterator.remove();
            }
            //只有工会之家需要协会管理
            if (websiteId != 4 && name.equals("协会管理")) {
                iterator.remove();
            }
            //工会之家不要组织管理
            if (websiteId == 4 && name.equals("组织管理")) {
                iterator.remove();
            }
        }
        List<Integer> rightIdList = userRightList.stream().map(Right::getId).collect(Collectors.toList());
        rightIdList.forEach(s -> {
            if (s == 2) {
                judgeMap.put("content", true);
            }
            if (s == 4) {
                judgeMap.put("column", true);
            }
            if (s == 5) {
                judgeMap.put("media", true);
            }
        });
        return ResultVO.data(judgeMap);
    }


    @Override
    public ResultVO getUserWebMenu(Integer websiteId, String userId) {
        List<Menu> menuList;
        if (Constant.SUPERADMINUSERID.equals(userId)) {
            menuList = menuMapper.selectList(new LambdaQueryWrapper<Menu>().ne(Menu::getIsDelete, 1));
            return roleService.getMenuTree(menuList);
        } else {
            menuList = roleRefMenuMapper.getUserWebMenu(userId, websiteId);
            return roleService.getMenuTree(menuList);
        }
    }

    @Override
    public ResultVO getWebInfoByUserId(String userId) {
        //通过用户id查询网站id
        List<WebsiteVo> websiteList = new ArrayList<>();
        if (Constant.SUPERADMINUSERID.equals(userId)) {
            List<Website> websites = websiteMapper.selectList(new QueryWrapper<Website>().eq("status", 1));
            for (Website website : websites) {
                WebsiteVo websiteVo = new WebsiteVo();
                websiteVo.setWebsiteId(website.getId());
                websiteVo.setWebsiteName(website.getWebsiteName());
                websiteList.add(websiteVo);
            }
        } else {
            websiteList = userMapper.getWebsiteListByUserId(userId);
        }
        return ResultVO.data(websiteList);
    }

    @Override
    public ResultVO getCenterInfoByCenterId(Integer centerId) {

        String websiteIdStr = WebsiteUtil.getWebsiteId();
        List<User> userListByWebsiteId = userMapper.getCenterUserListByWebsiteId(Integer.parseInt(websiteIdStr));

        if (centerId == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        List<User> userList = userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getCenterId, centerId));
        ArrayList<User> users = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userList)) {
            userList.forEach(s -> {
                Integer exist = userMapper.judgeRightExist(s.getId());
                if (exist != null) {
                    users.add(s);
                }
            });
        }
        List<User> userList1 = (List<User>) CollectionUtils.subtract(users, userListByWebsiteId);
        return ResultVO.data(userList1);
    }

    @Override
    public ResultVO judgeUserIsAdmin(String userId) {
        if (userId == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        boolean flag = true;
        if (Constant.SUPERADMINUSERID.equals(userId)) {
            return ResultVO.data(flag);
        }
        Integer integer = userMapper.judgeIsAdmin(userId);
        if (integer == null) {
            flag = false;
        }
        return ResultVO.data(flag);
    }
}

